package cn.hx.mallorder.controller;

import cn.hx.mallorder.dao.OmsCartItemDao;
import cn.hx.mallorder.entity.OmsCartItem;
import cn.hx.mallorder.util.JwtUtils;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 购物车表(CartItem)表控制层
 *
 * @author makejava
 * @since 2022-11-30 16:05:54
 */
@Controller
public class CartItemController {
    /**
     * 服务对象
     */
    @Resource
    private OmsCartItemDao cartItemDao;
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @GetMapping("cart")
    public String cart(Model model, HttpServletRequest request) {
        UmsMemberVo user = dataToCart(request);
        model.addAttribute("cartItems",
                cartItemDao.selectList(new QueryWrapper<OmsCartItem>().eq("member_id", user.getId())));
        return "cart";
    }

    @GetMapping("carts")
    @ResponseBody
    public List<OmsCartItem> cart(HttpServletRequest request) {
        UmsMemberVo user = dataToCart(request);
        return cartItemDao.selectList(new QueryWrapper<OmsCartItem>().eq("member_id", user.getId()));
    }

    private UmsMemberVo dataToCart(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id = null;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)) {
                        Claims claims = JwtUtils.getClaims(value);
                        id =  Long.valueOf(claims.get("data", Map.class).get("id").toString());
                        System.out.println(id);
                    }
                }
            }
        }
        UmsMemberVo user = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        assert user != null;
        return user;
    }

    @PostMapping("cart/updateQuantity")
    @ResponseBody
    public int updateQuantity(Integer id, String status) {
        OmsCartItem cartItem = cartItemDao.selectById(id);
        if (status.equals("dec")) {
            cartItem.setQuantity(cartItem.getQuantity() - 1);
        } else {
            cartItem.setQuantity(cartItem.getQuantity() + 1);
        }
        return cartItemDao.update(cartItem, new UpdateWrapper<OmsCartItem>().eq("id", cartItem.getId()));
    }

    @PostMapping("cart/del")
    public String del(Integer id) {
        System.out.println(id);
        cartItemDao.deleteById(id);
        return "redirect:/cart";
    }

    @GetMapping("cart/clear")
    public String clearCart(Long id) {
        QueryWrapper<OmsCartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", 2L);
        cartItemDao.delete(queryWrapper);
        return "redirect:/cart";
    }
}